/**
 * Project Name:template-web
 * File Name   :BaseServiceImpl.java
 * Package Name:org.cn.template.component.service
 * Date:2015年9月27日下午3:54:20
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.service;

import java.util.List;
import java.util.Map;

import org.cn.template.component.domain.PersistentModel;
import org.cn.template.component.page.PagerModel;
import org.cn.template.component.page.PagerUtil;
import org.cn.template.component.page.PagerUtil.IPager;
import org.cn.template.component.repository.IBaseDao;

/**
 * ClassName:BaseServiceImpl <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason :TODO ADD REASON. <br/>
 * Date :2015年9月27日 下午3:54:20 <br/>
 * 
 * @author :http://my.oschina.net/httpssl
 * @email :491835898@QQ.COM
 * @since :JDK 1.7
 * @see
 */
public abstract class BaseServiceImpl<T extends PersistentModel> implements IBaseService<T>
{
	protected abstract IBaseDao<T> getBaseDao();

	@Override
	public T insertOne(T model) throws Exception {
		return getBaseDao().insertOne(model);
	}

	@Override
	public void insertBatch(List<T> models) throws Exception {
		getBaseDao().insertBatch(models);
	}

	@Override
	public int deleteById(@SuppressWarnings("rawtypes")Map param) throws Exception {
		return getBaseDao().deleteById(param);
	}

	@Override
	public int delete(T param) throws Exception {
		// TODO Auto-generated method stub
		return getBaseDao().delete(param);
	}

	@Override
	public int updateById(T model) throws Exception {
		return getBaseDao().updateById(model);
	}

	@Override
	public <V extends T> V selectById(@SuppressWarnings("rawtypes")Map param) throws Exception {
		return getBaseDao().selectById(param);
	}

	@Override
	public <V extends T> List<V> selectList(T query) throws Exception {
		return getBaseDao().selectList(query);
	}

	@Override
	public <V extends T> List<V> selectList(T query,@SuppressWarnings("rawtypes")Map pageable) throws Exception {
		return getBaseDao().selectList(query, pageable);
	}

	@Override
	public <V extends T> V selectOne(T query) throws Exception {
		return getBaseDao().selectOne(query);
	}

	@Override
	public Long selectListCount(T query) throws Exception {
		return getBaseDao().selectListCount(query);
	}

	@Override
	public <V extends T> List<V> selectList(String statement,@SuppressWarnings("rawtypes")Map map) throws Exception {
		// TODO Auto-generated method stub
		return getBaseDao().selectList(statement, map);
	}

	@Override
	public List<?> selectMapList(String statement,@SuppressWarnings("rawtypes")Map map) throws Exception {
		return getBaseDao().selectMapList(statement, map);
	}

	@Override
	public PagerModel getPagerModel(@SuppressWarnings("rawtypes")Map paramMap) throws Exception {
		return this.getPagerModel(paramMap, this.getBaseDao());
	}

	private PagerModel getPagerModel(@SuppressWarnings("rawtypes")Map paramMap, IBaseDao<T> baseDao) throws Exception {
		return this.getPagerModel(paramMap, SqlMap_Page_prefix, baseDao);
	}

	private PagerModel getPagerModel(@SuppressWarnings("rawtypes")Map paramMap, final String statement, final IBaseDao<T> baseDao) throws Exception {
		return PagerUtil.getPagerModel(new IPager()
		{
			@Override
			public List<?> getList(Map<String, ?> paramMap) throws Exception {
				return baseDao.getList(statement, paramMap);
			}

			@Override
			public Long getListCount(Map<String, ?> paramMap) throws Exception {
				return baseDao.getListCount(statement + SqlMap_Page_Count_Suffix, paramMap);
			}
		}, paramMap);
	}

	/*
	 * 自定义扩展分页. statement 你的SqlMap id
	 * 
	 * 一个 statement 一个 statementList
	 */
	public PagerModel getPagerModel(final String statement, Map<String, Object> paramMap) throws Exception {
		return PagerUtil.getPagerModel(new IPager()
		{
			@Override
			public List<?> getList(Map<String, ?> paramMap) throws Exception {
				return getBaseDao().getList(statement, paramMap);
			}

			@Override
			public Long getListCount(Map<String, ?> paramMap) throws Exception {
				return getBaseDao().getListCount(statement + SqlMap_Page_Count_Suffix, paramMap);
			}
		}, paramMap);
	}

	private final String	SqlMap_Page_Count_Suffix	= "Count";
	private final String	SqlMap_Page_prefix			= "selectList";
}
